#!/bin/bash

export SCRIPT_DIR=$(dirname "$0")

##
## Configuration Variables
##

# The name of the keychain to create for iOS code signing.
KEYCHAIN=ios-build.keychain

##
## Build Process
##

main ()
{
    import_certs

    make test package
    local status=$?

    delete_keychain
    exit $status
}

import_certs ()
{
    # If this environment variable is missing, we must not be running on Travis.
    if [ -z "$KEY_PASSWORD" ]
    then
        return 0
    fi

    echo "*** Setting up code signing..."
    local password=cibuild

    # Create a temporary keychain for code signing.
    security create-keychain -p "$password" "$KEYCHAIN"
    security default-keychain -s "$KEYCHAIN"
    security unlock-keychain -p "$password" "$KEYCHAIN"
    security set-keychain-settings -t 3600 -l "$KEYCHAIN"

    # Download the certificate for the Apple Worldwide Developer Relations
    # Certificate Authority.
    local certpath="$SCRIPT_DIR/apple_wwdr.cer"
    curl 'https://developer.apple.com/certificationauthority/AppleWWDRCA.cer' > "$certpath"
    security import "$certpath" -k "$KEYCHAIN" -T /usr/bin/codesign

    # Import our development certificate.
    security import "$SCRIPT_DIR/certificates/development.p12" -k "$KEYCHAIN" -P "$KEY_PASSWORD" -T /usr/bin/codesign
}

delete_keychain ()
{
    if [ -z "$KEY_PASSWORD" ]
    then
        return 0
    fi

    security delete-keychain "$KEYCHAIN"
}

export -f import_certs
export -f delete_keychain

main
